package com.xh.demo.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import com.xh.demo.domain.properties.JdbcProperties;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.Properties;

/**
 * @Name AppConfig
 * @Description applicationContext配置
 * @Author wen
 * @Date 2019-07-31
 */
@Configuration
@EnableTransactionManagement        //开启基于注解的事务管理
@ComponentScan(basePackages = {"com.xh.demo"}, excludeFilters = {
        @ComponentScan.Filter(type = FilterType.REGEX,
                pattern = {"com.xh.demo.controller"}),
})
public class AppConfig {

    /**
     * 获取上下文中的类配置
     **/
//    @Bean
//    public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer(){
//        return new PropertyPlaceholderConfigurer();
//    }

    /**
     * 配置数据源
     **/
    @Bean
    public DataSource dataSource(JdbcProperties jdbcProperties) {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(jdbcProperties.getUrl());
        dataSource.setDriverClassName(jdbcProperties.getDriverClass());
        dataSource.setUsername(jdbcProperties.getUsername());
        dataSource.setPassword(jdbcProperties.getPassword());
        dataSource.setInitialSize(jdbcProperties.getInitialSize());
        dataSource.setMinIdle(jdbcProperties.getMinIdle());
        dataSource.setMaxActive(jdbcProperties.getMaxActive());
        dataSource.setMaxWait(jdbcProperties.getMaxWait());
        return dataSource;
    }

    /**
     * 配置SqlSession工厂
     **/
    @Bean
    public FactoryBean sqlSessionFactoryBean(DataSource dataSource) throws IOException {
        PathMatchingResourcePatternResolver classPathResource = new PathMatchingResourcePatternResolver();
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setMapperLocations(classPathResource.getResources("classpath:/mappers/*.xml"));
        sqlSessionFactoryBean.setConfigLocation(classPathResource.getResource("classpath:/mybatis-config.xml"));
        //配置分页插件
        PageInterceptor pageInterceptor = new PageInterceptor();
        Properties properties = new Properties();
        properties.setProperty("helperDialect", "mysql");
        properties.setProperty("offsetAsPageNum", "true");
        properties.setProperty("pageSizeZero", "true");
        properties.setProperty("supportMethodsArguments", "true");
        properties.setProperty("reasonable", "true");
        pageInterceptor.setProperties(properties);
        sqlSessionFactoryBean.setPlugins(new Interceptor[]{});
        return sqlSessionFactoryBean;
    }

    /**
     * 配置通用mapper扫描路径
     **/
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer configurer = new MapperScannerConfigurer();
        configurer.setBasePackage("com.xh.demo.dao.mapper");
        return configurer;
    }

    /**
     * 将事务管理器注册到IOC容器中
     **/
    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

}
